﻿using System;
using System.Data;
using Koonsoft.HYCRM.DAL;
using log4net;

namespace Koonsoft.HYCRM.BLL
{
    public class PaymentBLL
    {
        private ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private PaymentDAL _companyDAL = null;

        protected PaymentDAL DAL
        {
            get
            {
                if (_companyDAL == null)
                {
                    _companyDAL = new PaymentDAL();
                }

                return _companyDAL;
            }
        }

        /// <summary>
        /// 新增付款记录
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="payType"></param>
        /// <param name="payDate"></param>
        /// <param name="amount"></param>
        /// <param name="remark"></param>
        /// <param name="bank"></param>
        /// <param name="empolyeeID"></param>
        /// <param name="errorMessage"></param>
        public void AddPaymentRecord(string orderID, int payType, DateTime payDate, int amount, int bank, string remark, int empolyeeID, out string errorMessage)
        {
            errorMessage = string.Empty;

            try
            {


                #region 检查订单是否存在

                var order = new OrderBLL().GetOrderInfoByOrderID(orderID);
                if (order == null)
                {
                    errorMessage = string.Format("单号为'{0}'的订单在数据库中找不到记录", orderID);
                    return;
                }
                #endregion

                #region 检查支付金额是否超出订单总金额的120%
                if (amount > (order.Amount * 1.2))
                {
                    errorMessage = string.Format("订单总金额为{0}，付款数超出订单金额的120%", order.Amount);
                    return;
                }

                #endregion

                switch (payType)
                {
                    // 首款
                    case 1:
                        #region 判断该单是否已付首款，已付首款则不能再付首款
                        if (DAL.HasPayFirst(orderID))
                        {
                            errorMessage = "该单已付首款，不能再度付款";
                            return;
                        }
                        #endregion

                        #region 增加付款记录
                        DAL.AddPaymentRecord(orderID, payType, payDate, amount, bank, remark);
                        #endregion

                        DAL.BeginPay(orderID);
                        break;

                    // 中间款
                    case 2:
                        if (!DAL.HasPayFirst(orderID))
                        {
                            errorMessage = "该单未付首款，请先付首款";
                            return;
                        }
                        #region 增加付款记录
                        DAL.AddPaymentRecord(orderID, payType, payDate, amount, bank, remark);
                        #endregion
                        break;

                    // 尾款
                    case 3:
                        #region 判断是否已结尾款
                        if (DAL.HasPayOver(orderID))
                        {
                            errorMessage = "该单已付尾款，不能再度付款";
                            return;
                        }
                        #endregion

                        #region 增加付款记录
                        DAL.AddPaymentRecord(orderID, payType, payDate, amount, bank, remark);
                        #endregion

                        #region 如果所有支付数据大于等于成交价，则更改订单状态，流转到下一步文件处理；只有结了尾款才能改变订单状态
                        var audit = DAL.SumByOrderID(orderID);

                        if (audit >= 0)
                        {
                            DAL.FinishPay(orderID, empolyeeID);
                        }
                        #endregion

                        break;

                    // 多缴款、少缴款
                    case 4:
                    case 5:
                        #region 增加付款记录
                        DAL.AddPaymentRecord(orderID, payType, payDate, amount, bank, remark);
                        #endregion

                        break;
                }
            }
            catch (Exception ex)
            {
                logger.Error("新增付款记录异常", ex);
            }
        }

        /// <summary>
        /// 根据订单编号获取交易记录
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public DataTable GetTransactionTrackByOrderID(string orderID)
        {
            return DAL.GetTransactionTrackByOrderID(orderID);
        }
    }
}
